package cn.iocoder.yudao.module.wecom.controller.admin.taggroup;

import cn.iocoder.yudao.module.wecom.controller.admin.tags.vo.WeTagsRespVO;
import cn.iocoder.yudao.module.wecom.service.tags.WeTagsService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.wecom.controller.admin.taggroup.vo.*;
import cn.iocoder.yudao.module.wecom.dal.dataobject.taggroup.WeTagGroupDO;
import cn.iocoder.yudao.module.wecom.service.taggroup.WeTagGroupService;

@Tag(name = "管理后台 - 标签分组")
@RestController
@RequestMapping("/wecom/we-tag-group")
@Validated
public class WeTagGroupController {

    @Resource
    private WeTagGroupService weTagGroupService;

    @Resource
    private WeTagsService weTagsService;

    @PostMapping("/create")
    @Operation(summary = "创建标签分组")
    @PreAuthorize("@ss.hasPermission('wecom:we-tags:create')")
    public CommonResult<Boolean> createWeTagGroup(@Valid @RequestBody WeTagGroupSaveReqVO createReqVO) {
        weTagGroupService.createWeTagGroup(createReqVO);
        return success(true);
    }

    @PostMapping("/sync-tags")
    @Operation(summary = "同步企微标签")
    @PreAuthorize("@ss.hasPermission('wecom:we-tags:create')")
    public CommonResult<Boolean> syncTags() {
        weTagGroupService.syncTags();
        return success(true);
    }

    @PutMapping("/update")
    @Operation(summary = "更新标签分组")
    @PreAuthorize("@ss.hasPermission('wecom:we-tags:update')")
    public CommonResult<Boolean> updateWeTagGroup(@Valid @RequestBody WeTagGroupSaveReqVO updateReqVO) {
        weTagGroupService.updateWeTagGroup(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除标签分组")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wecom:we-tags:delete')")
    public CommonResult<Boolean> deleteWeTagGroup(@RequestParam("id") Long id) {
        weTagGroupService.deleteWeTagGroup(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得标签分组")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wecom:we-tags:query')")
    public CommonResult<WeTagGroupRespVO> getWeTagGroup(@RequestParam("id") Long id) {
        WeTagGroupDO weTagGroup = weTagGroupService.getWeTagGroup(id);
        WeTagGroupRespVO result = BeanUtils.toBean(weTagGroup, WeTagGroupRespVO.class);
        if (Objects.isNull(result)) {
            return success(null);
        }
        result.setTagList(BeanUtils.toBean(weTagsService.getWeTagsByGroupId(result.getGroupId()), WeTagsRespVO.class));
        return success(result);
    }

    @GetMapping("/tree-list")
    @Operation(summary = "获得标签分组")
    public CommonResult<List<WeTagGroupTreeRespVO>> getWeTagGroupTreeList() {
        List<WeTagGroupDO> weTagGroup = weTagGroupService.getWeTagGroupList();
        List<WeTagGroupTreeRespVO> result = BeanUtils.toBean(weTagGroup, WeTagGroupTreeRespVO.class);
        result.forEach(item -> {
            item.setTags(BeanUtils.toBean(weTagsService.getWeTagsByGroupId(item.getGroupId()), WeTagsRespVO.class));
        });
        return success(result);
    }


    @GetMapping("/page")
    @Operation(summary = "获得标签分组分页")
    @PreAuthorize("@ss.hasPermission('wecom:we-tags:query')")
    public CommonResult<PageResult<WeTagGroupRespVO>> getWeTagGroupPage(@Valid WeTagGroupPageReqVO pageReqVO) {
        PageResult<WeTagGroupDO> pageResult = weTagGroupService.getWeTagGroupPage(pageReqVO);
        PageResult<WeTagGroupRespVO> result = BeanUtils.toBean(pageResult, WeTagGroupRespVO.class);
        result.getList().forEach(item -> {
            item.setTagList(BeanUtils.toBean(weTagsService.getWeTagsByGroupId(item.getGroupId()), WeTagsRespVO.class));
        });
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出标签分组 Excel")
    @PreAuthorize("@ss.hasPermission('wecom:we-tags:export')")
    @OperateLog(type = EXPORT)
    public void exportWeTagGroupExcel(@Valid WeTagGroupPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<WeTagGroupDO> list = weTagGroupService.getWeTagGroupPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "标签分组.xls", "数据", WeTagGroupRespVO.class,
                        BeanUtils.toBean(list, WeTagGroupRespVO.class));
    }

}